package source.view;

import source.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class UnitOverview extends JPanel{
        
        /********* DATA MEMBERS **********/
        private PlayerStatus player;

        //This is the number of different Unit Attributes
        private final int numUnitAttributes = 9;

	private Controller controller;

	private Unit[] playersUnits;

	class ourTable extends JTable{
		public ourTable(Object[][] data, String[] columnNames){
			super(data, columnNames);
			super.addKeyListener(new tableKeys());
		}
						
	}

	private ourTable table;
	
	class tableKeys implements KeyListener{
		public void keyPressed(KeyEvent e){
			switch(e.getKeyCode()){
				case KeyEvent.VK_M:
					Unit[] armyunits = new Unit[playersUnits.length];
					int armyunitsindex = 0;
					int xcoord = -1;
					int ycoord = -1;
					boolean armyfucked = false;
					for(int i = 0; i < playersUnits.length; i++){
						if(table.isRowSelected(i)){
							if(xcoord == -1){
								xcoord = playersUnits[i].getPosition().getX();
								ycoord = playersUnits[i].getPosition().getY();
								
								armyunits[armyunitsindex] = playersUnits[i];
								armyunitsindex++;
							} else{
								if(xcoord == playersUnits[i].getPosition().getX() && ycoord == playersUnits[i].getPosition().getY()){
									armyunits[armyunitsindex] = playersUnits[i];
									armyunitsindex++;
								}else{
									armyfucked = true;
								}
							}
						}
					}

					if(xcoord != -1 && !armyfucked){
						Unit[] armyunits2 = new Unit[armyunitsindex];

						System.arraycopy(armyunits, 0, armyunits2, 0, armyunitsindex);
						Army a = new Army(armyunits2, player, armyunits2[0].getPosition());
						updateOverview();
					}
					break;
				case KeyEvent.VK_D:
				case KeyEvent.VK_B:
					for(int i = 0; i < playersUnits.length; i++){
						if(table.isRowSelected(i)){
							if(playersUnits[i].getUnitType() == UnitType.ARMY){
								((Army)(playersUnits[i])).breakArmy();
							}
						}
					}

					updateOverview();
					break;
				default:
					controller.keyPressed(e);
			}
		}

		public void keyReleased(KeyEvent e){
		}

		public void keyTyped(KeyEvent e){
		}
	}

	class uneditableCellsModel extends AbstractTableModel{
		private String[] columnNames;
		private Object[][] rowData;

		public uneditableCellsModel(String[] columnsNames, Object[][] data){
			this.columnNames = columnsNames;
			this.rowData = data;
		}

		public boolean isCellEditable(int row, int col){
			return false;
		}
		public String getColumnName(int col){
			return columnNames[col].toString();
		}
		public int getRowCount(){
			return rowData.length;
		}
		public int getColumnCount(){
			return columnNames.length;
		}
		public Object getValueAt(int row, int col){
			return rowData[row][col];
		}
		public void setValueAt(Object value, int row, int col){
			rowData[row][col] = value;
			fireTableCellUpdated(row, col);
		}
	}

        /********* CONSTRUCTORS *********/
        public UnitOverview(){
                player = null;
        }

        public UnitOverview(Controller controller){
		this.controller = controller;
	}
	
	public UnitOverview(PlayerStatus player, Controller controller){
                this.player = player;
        	this.controller = controller;
	}



        /********* PUBLIC FUNCTIONS ***********/
	public void setCurrentPlayer(PlayerStatus player){
		this.player = player;
	}

        public void updateOverview(){
                //Clears the current overview
                removeAll();

                if(player == null)
                        return;

		playersUnits = player.getUnitsArray();

		setLayout(new GridLayout(1, 1));


		String[] columnNames = {"Type", "CurHP/MaxHP", "View Range", "Off. Damage", "Def. Damage", "X,Y", "Moves Left", "Movement Rate", "Mission X,Y"};

		final Object[][] data = new Object[playersUnits.length][numUnitAttributes];
                for(int i = 0; i < playersUnits.length; i++){
     
			Unit unit = playersUnits[i];
 
			UnitType uType = unit.getUnitType();
			
			String unitName = "";

			switch(uType){
				case ARMY:
					unitName = "Army";
					break;
				case COLONIST:
					unitName = "Colonist";
					break;
				case EXPLORER:
					unitName = "Explorer";
					break;
				case MELEE:
					unitName = "Melee";
					break;
				case RANGED:
					unitName = "Ranged";
					break;
			}	
 
			data[i][0] = unitName;


			data[i][1] = unit.getCurrentHP() + "/" + unit.getMaxHP();

			data[i][2] = unit.getViewDistance() + "";

			data[i][3] = unit.getAttackPower() + "";
		
			data[i][4] = unit.getDefensivePower() + "";

			data[i][5] = unit.getPosition().getX() + "," + unit.getPosition().getY();

			data[i][6] = unit.getRemainingMoves() + "";

			data[i][7] = unit.getMovementRate() + "";

			Tile destTile = unit.getDestinationTile();

			if(destTile == null)
				data[i][8] = "None";
			else   
				data[i][8] = unit.getDestinationTile().getX() + "," + unit.getDestinationTile().getY();

                }
        

		

		table = new ourTable(data, columnNames);
		uneditableCellsModel ucm = new uneditableCellsModel(columnNames, data);
		table.setModel(ucm);
		table.removeEditor();

		JScrollPane scrollpane = new JScrollPane(table);
		add(scrollpane);
		validate();
		repaint();
	}

}
